/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import game.Spiel;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author timbeer
 */
public class Server extends Thread{

    int port;
    private Hashtable outputStreams = new Hashtable();
    private ServerSocket server;
    private boolean ServerStatus = true;

    
    Spiel spiel = new Spiel();

    public Server(int port) throws IOException {
        this.port = port;
    }

 
    //Get an enumeration of all the OutputStreams, one for each client connected to the server

    Enumeration getOutputStreams() {
        return outputStreams.elements();
    }

    //Send object to all the clients
    public void sendToAll(Spiel spiel) {

        // We synchronize on this because another thread might be
        // calling removeConnection() and this would screw us up
        // as we tried to walk through the list
        synchronized (outputStreams) {

            //for each client
            for (Enumeration e = getOutputStreams(); e.hasMoreElements();) {

                //get the output stream
                ObjectOutputStream oos = (ObjectOutputStream) e.nextElement();

                //send the object
                try {
                    oos.writeObject(spiel);
                    
                    System.out.println("Object sent");

                } catch (IOException ie) {
                    System.out.println(ie);
                }
            }

        }
    }

    // Remove a socket, and it's corresponding output stream, from our
    // list.  This is usually called by a connection thread that has
    // discovered that the connectin to the client is dead.
    public void removeConnection(Socket client) {
        // Synchronize so we don't mess up sendToAll() while it walks
        // down the list of all output streamsa
        synchronized (outputStreams) {
            // Tell the world
            System.out.println("Removing connection to " + client);
            // Remove it from our hashtable/list
            outputStreams.remove(client);
            // Make sure it's closed
            try {
                client.close();
            } catch (IOException ie) {
                System.out.println("Error closing " + client);
                ie.printStackTrace();
            }
        }
    }
    
    public void setServerStatus(boolean ServerStatus){
        this.ServerStatus = ServerStatus;
    }
    
    public void run(){
        try{  
        server = new ServerSocket(port);
        System.out.println("Server is listening on port 7777");
        
        while(outputStreams.size() <= 4){

            Socket client = server.accept();

            System.out.println("Verbindung zum client hergestellt.");

            ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());

            outputStreams.put(client, oos);

            new ServerThread(this, client);

     
        }
        
    }catch(Exception e){
        }
    
}
}
